package com.dennytech.tac.base;

import android.app.Application;

import com.dennytech.tac.net.HttpBase;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.utils.Log;
import com.dennytech.tac.utils.db.ChannelDBHelper;
import com.dennytech.tac.utils.db.CityIntroDBHelper;
import com.dennytech.tac.utils.db.TimelineDBHelper;
import com.dennytech.tac.utils.db.TogoDBHelper;
import com.dennytech.tac.widget.NetworkPhotoView;
import com.dennytech.tac.widget.NetworkThumbView;

public class CFApplication extends Application {

	private static CFApplication instance;

	public static CFApplication instance() {
		if (instance == null) {
			throw new IllegalStateException("Application has not been created");
		}

		return instance;
	}

	static CFApplication _instance() {
		return instance;
	}

	public CFApplication() {
		instance = this;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initEnvironment();
	}

	@Override
	public void onTerminate() {
		CityIntroDBHelper.instance().close();
		// AttractionDBHelper.instance().close();
		// TravelGuideDBHelper.instance().close();
		ChannelDBHelper.instance().close();
		TimelineDBHelper.instance().close();
		TogoDBHelper.instance().close();

		Stat.uninit();
		super.onTerminate();
	}

	private void initEnvironment() {
		CityIntroDBHelper.init(this);
		// AttractionDBHelper.init(this);
		// TravelGuideDBHelper.init(this);
		ChannelDBHelper.init(this);
		TimelineDBHelper.init(this);
		TogoDBHelper.init(this);

		if ((getApplicationInfo().flags & 2) != 0) {
			// ApplicationInfo.FLAG_DEBUGGABLE
			Log.LEVEL = Log.VERBOSE;
		} else {
			Log.LEVEL = Integer.MAX_VALUE;
		}

		HttpBase.init(this);
		NetworkThumbView.init(this);
		NetworkPhotoView.init(this);
		Stat.init(this);
		Environment.init(this);
	}

}
